package com.sunfeng.chap2.p13;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author : 孙峰
 * @Description: 独占锁是可以自己写的
 * @Date : 2021/10/9  16:52
 */
public class Test13_12 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("d:\\nio\\d.txt", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        fileChannel.lock(1, 2, false);
        ByteBuffer byteBuffer = ByteBuffer.wrap("123456".getBytes());
        fileChannel.write(byteBuffer);
        fileChannel.close();
        randomAccessFile.close();
    }
}
